package com.tcm.doctor.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.base.core.codec.Base64Utils;
import com.base.core.codec.DesUtils;
import com.base.core.lang.DateUtils;
import com.base.core.utils.JacksonUtils;
import com.base.core.utils.R;
import com.tcm.doctor.domain.entity.Doctor;
import com.tcm.doctor.domain.enums.BusinessCodeEnum;
import com.tcm.doctor.mapper.DoctorMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;


@DubboService(version = "0.0.1-alpha", delay = -1, retries = 3)
public class QrcodeServiceImpl implements QrcodeService {

    @Autowired
    private DoctorMapper doctorMapper;

    // 扫描二维码需要访问的URL，如果是想访问自己的App，则把相应的APP地址放到二维码中
    private static final String BASE_URL = "http://spread.zetikeji.com/invite/?action=friend";

    private static final String secretKey = "1234123sdu9du";

    /**
     * 生成二维码URL地址
     * @param paramMaps 上送字段map
     * @return
     * @throws Exception
     */
    private String generateUrl(Map<String, String> paramMaps) {
        String url = null;
        try {
            String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            String randomStr = "";
            String jsonStr = "";
            for (int i = 0; i < 10; i++) {
                randomStr = randomStr + chars.charAt((int) (Math.random() * 52));
            }
            jsonStr = JacksonUtils.toJSONString(paramMaps);
            String param = randomStr + DesUtils.encode(jsonStr +"@"+ DateUtils.getDateTime(), secretKey);
            url = BASE_URL + "&target=doctor&param=" + URLEncoder.encode(param, "utf8");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return url;
    }

    /**
     * 生成二维码
     *
     * @param userId 医生用户id
     * @return
     */
    @Override
    public Map<String, String> doctorCard(Long userId) {
        Doctor user = doctorMapper.findByUserId(userId);
        if (ObjectUtil.isEmpty(user)) {
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_NOT_EXISTS);
        }
        QrConfig qrConfig = new QrConfig();
        qrConfig.setCharset(Charset.defaultCharset());
        qrConfig.setMargin(1);
        qrConfig.setHeight(210);
        qrConfig.setWidth(210);
        Map<String, String> maps = new HashMap<>();
        maps.put("doctorUserId", userId.toString());
        byte[] bytes = QrCodeUtil.generatePng(generateUrl(maps), qrConfig);
        String base64 = Base64Utils.simpleEncode(bytes);
        Map<String, String> map = new HashMap<String, String>();
        map.put("qrcode", base64);
        return map;
    }

    /**
     * 生成邀请链接
     *
     * @param userId 医生用户id
     * @return
     */
    @Override
    public String doctorCardUrl(Long userId) {
        Doctor user = doctorMapper.findByUserId(userId);
        if (ObjectUtil.isEmpty(user)) {
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_NOT_EXISTS);
        }
        Map<String, String> maps = new HashMap<>();
        maps.put("doctorUserId", userId.toString());
        return generateUrl(maps);
    }


}
